﻿using Selenium;

namespace Quail
{
    /// <summary>
    /// A bastic test session implementation, using BasicWebBrowser
    /// </summary>
    public class BasicTestSession : TestSession
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="BasicTestSession"/> class.
        /// </summary>
        /// <param name="shell">The test shell.</param>
        /// <param name="sessionName">The session name.</param>
        public BasicTestSession(BasicTestShell shell, string sessionName) : base(sessionName)
        {
            Shell = shell;
        }


        /// <summary>
        /// Gets the test shell
        /// </summary>
        public BasicTestShell Shell
        {
            get; private set;
        }

        /// <summary>
        /// Gets or sets the test session's base url (usually the absolute part of the url)
        /// </summary>
        public override string BaseUrl
        {
            get
            {
                return base.BaseUrl ?? Shell.GetStartupUrl();
            }

            set
            {
                base.BaseUrl = value;
            }
        }

        /// <summary>
        /// Starts the browser session
        /// </summary>
        /// <param name="url">The url to open</param>
        /// <returns>The new web browser object</returns>
        public override IWebBrowser StartBrowser(string url)
        {
            Browser = CreateBrowser();
            SeleniumInterface = new BasicSeleniumInterface((BasicWebBrowser) Browser);
            Browser.Navigate(url);
            
            return Browser;
        }

        /// <summary>
        /// Gets an interface to Selenium or a Selenium-like implementation  
        /// BasicTestSession returns a BasicSeleniumInterface object - but currently this is just an idea, it doesn't actually work - so don't use it!
        /// </summary>
        public ISelenium SeleniumInterface
        {
            get; protected set;
        }

        /// <summary>
        /// Creates a new instance of the BasicWebBrowser
        /// </summary>
        /// <returns></returns>
        protected override IWebBrowser CreateBrowser()
        {
            return new BasicWebBrowser(this, new BasicWebPage(Shell));
        }
    }
}
